package com.ruoyi.web.controller.system;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.Course;
import com.ruoyi.system.domain.vo.CourseVo;
import com.ruoyi.system.service.CourseService;
import org.apache.poi.ss.formula.functions.BaseNumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.ZoneId;
import java.util.List;

/**
 * 课程信息表(Course)表控制层
 *
 * @author makejava
 * @since 2024-05-10 16:15:23
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    /**
     * 分页查询
     *
     * @param course     筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> courses = courseService.queryList(course);
        return getDataTable(courses);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param courseId 主键
     * @return 单条数据
     */
    @GetMapping(value = "/lesson/{courseId}")
    public AjaxResult queryByCourseId(@PathVariable("courseId") Long courseId) {
        return success(courseService.queryByCourseId(courseId));
    }

    /**
     * 通过courseId查询数据
     *
     * @param courseId 主键
     * @return 单条数据
     */
    @GetMapping(value = "{courseId}")
    public AjaxResult queryById(@PathVariable("courseId") Long courseId) {
        return success(courseService.queryById(courseId));
    }



//    /**
//     * 新增数据
//     *
//     * @param course 实体
//     * @return 新增结果
//     */
//    @PostMapping
//    public AjaxResult add(@Validated @RequestBody CourseVo courseVo){
//        courseVo.setCreateBy(getUsername());
//        courseVo.setCreateTime(DateUtils.getNowDate());
//        return success(courseService.insert(courseVo));
//    }
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Course course){
        course.setCreateBy(getUsername());
        course.setCreateTime(
                Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime()
        );
        return success(courseService.insert(course));
    }



//    /**
//     * 编辑数据
//     *
//     * @param courseVo 实体
//     * @return 编辑结果
//     */
//    @PutMapping 解决Date无法转换为DataTime
//    public AjaxResult edit(@Validated @RequestBody CourseVo courseVo) {
//        courseVo.setUpdateBy(getUsername());
//        courseVo.setUpdateTime(DateUtils.getNowDate());
//        return success(courseService.update(courseVo));
//    }
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Course course) {
        course.setUpdateBy(getUsername());
        course.setUpdateTime(
                Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime()
        );
        return success(courseService.update(course));
    }

    /**
     * 删除数据
     *
     * @param courseId 主键
     * @return 删除是否成功
     */
    @DeleteMapping(value = "{courseId}")
    public AjaxResult deleteById(@PathVariable String courseId) {
        return success(courseService.deleteById(Long.valueOf(courseId)));
    }

    @PostMapping("/img")
    public AjaxResult avatar(@RequestParam("imgfile") MultipartFile file,@RequestParam("id") Long id) throws Exception
    {
        if (!file.isEmpty())
        {
            String headImg = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/headImg", file, MimeTypeUtils.IMAGE_EXTENSION);
            return success(courseService.updateById(id,headImg));
        }
        return error("上传图片异常，请联系管理员");
    }



}

